/**
 * 文件名：****.java
 * 版权：Copyright Lightkit All Rights Reserved.
 * 描述：*****
 */
package com.lightkits.wms.domain.deliveryticket;

import com.lightkits.framework.common.exception.LightKitsBizException;
import com.lightkits.framework.common.model.IResult;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.framework.soa.hzero.executor.ServiceExecutor;
import com.lightkits.wms.domain.deliveryticket.service.SaveTicketLineDomain;
import com.lightkits.wms.dto.deliveryticket.SaveTicketLineRequestType;
import com.lightkits.wms.dto.deliveryticket.SaveTicketLineResponseType;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * SaveTicketLineExecutor
 *
 * @author jing.wei@hand-china.com
 * Created on 2019/4/23 14:36
 */
@Component
public class SaveTicketLineExecutor implements ServiceExecutor<SaveTicketLineRequestType, SaveTicketLineResponseType> {
    private final SaveTicketLineDomain saveTicketLineDomain;

    @Autowired
    public SaveTicketLineExecutor(SaveTicketLineDomain saveTicketLineDomain) {
        this.saveTicketLineDomain = saveTicketLineDomain;
    }

    @Override
    public SaveTicketLineResponseType service(SaveTicketLineRequestType request) {
        SaveTicketLineResponseType response = new SaveTicketLineResponseType();
        response.setSuccessful(true);
        try {
            IResult result = saveTicketLineDomain.saveTicketLine(
                    request.getTicketLineDTOS(),
                    request.getTicketId(),
                    request.getParentEventId());
            if (!result.isSuccessful()) {
                return result.fill(response);
            }
            result.fill(response);
        } catch (LightKitsBizException e) {
            response.setSuccessful(false);
            String mes = StringUtils.substringAfterLast(e.getMessage(), ":");
            response.setMessage(ObjectUtil.isNullOrEmpty(mes) ? e.getMessage() : mes);
        } catch (Exception e) {
            e.printStackTrace();
            response.setSuccessful(false);
            response.setMessage("Save Ticket Line Error");
        }
        return response;
    }
}
